import os
import threading
import random
from pythonwork.photo.RequestUtil import RequestUtil


class ThreadDown(threading.Thread):

    def __init__(self, data, thread_name):
        threading.Thread.__init__(self)
        self.data = data
        self.thread_name = thread_name

    def run(self):
        print('线程开始->' + self.thread_name)
        ThreadDown.down_list_url(self.data)
        print('线程结束->' + self.thread_name)

    @staticmethod
    def down_list_url(list_url):
        lib = 'd:/set2/'
        if not os.path.exists(lib):
            os.mkdir(lib)
        response_list = RequestUtil.get_list_request(list_url, 'content')
        for res in response_list:
            rand = random.randrange(0, 100000, 1)
            file_path = lib + str(rand) + '.jpg'
            file = open(file_path, "wb")
            file.write(res)
            file.close()
        print("文件下载完成")
